package win.smartown.scrollconflict.netease;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import win.smartown.scrollconflict.R;

/**
 * Created by Developer on 2017/11/17.
 */

public class NeteaseMusicActivity extends AppCompatActivity {

    private List<View> mViewList;

    private TabLayout mTabLayout;
    private OuterViewPager mOuterViewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_netease_music);
        mTabLayout = findViewById(R.id.netease_music_outer_tab);
        mOuterViewPager = findViewById(R.id.netease_music_outer_pager);
        mTabLayout.setupWithViewPager(mOuterViewPager);
        initViews();
    }

    private void initViews() {
        mViewList = new ArrayList<>();

        View view1 = LayoutInflater.from(this).inflate(R.layout.music_pager_1, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.music_pager_2, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.music_pager_3, null);

        Pager2 pager2 = new Pager2(view2);
        pager2.mInnerViewPager.setOuterViewPager(mOuterViewPager);

        mViewList.add(view1);
        mViewList.add(pager2.mView);
        mViewList.add(view3);

        mOuterViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mViewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return (position + 1) + "";
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViewList.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mViewList.get(position));
            }
        });
    }

    private final static class Pager2 {

        private View mView;
        private TabLayout mTabLayout;
        private InnerViewPager mInnerViewPager;

        public Pager2(View view) {
            this.mView = view;
            mTabLayout = view.findViewById(R.id.pager_2_tab);
            mInnerViewPager = view.findViewById(R.id.pager_2_view_pager);
            mTabLayout.setupWithViewPager(mInnerViewPager);
            mInnerViewPager.setAdapter(new PagerAdapter() {
                @Override
                public int getCount() {
                    return 3;
                }

                @Nullable
                @Override
                public CharSequence getPageTitle(int position) {
                    return (position + 1) + "";
                }

                @Override
                public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                    return false;
                }

                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    TextView textView = new TextView(container.getContext());
                    textView.setText("" + position);
                    container.addView(textView);
                    return textView;
                }

                @Override
                public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                    container.removeView((View) object);
                }
            });
        }
    }

}
